#include "TitleBarAbstract.h"
#include <QPainter>
#include <QPen>
#include <QMouseEvent>

TitleBarAbstract::TitleBarAbstract(QWidget *parent) :
	QWidget(parent),
	_cacheImage(NULL)
{
	setMouseTracking(true);
}

TitleBarAbstract::~TitleBarAbstract()
{
	if(_cacheImage) delete _cacheImage;
}

void TitleBarAbstract::resizeEvent(QResizeEvent *event)
{
	if(_cacheImage) delete _cacheImage;
	_cacheImage = new QPixmap(size());
	_cacheImage->fill(Qt::transparent);
	QPainter painter(_cacheImage);

	paintHeader(&painter);
}

void TitleBarAbstract::paintEvent(QPaintEvent *event)
{
	if(_cacheImage != NULL){
		QPainter painter(this);
		painter.drawPixmap(0, 0, *_cacheImage);
	}
}

void TitleBarAbstract::paintHeader(QPainter* painter)
{
	int radius = 8;
	QPainterPath path;

	path.moveTo(0,height());
	path.lineTo(0,radius-1);
	path.arcTo(0,0,2*radius,2*radius,180,-90);
	path.lineTo(width()-radius-1,0);
	path.arcTo(width()-2*radius-1,0,2*radius,2*radius,90,-90);
	path.lineTo(width()-1,height());

	painter->setPen(Qt::black);

	painter->fillPath(path,QColor(220,220,220));
	painter->drawPath(path);
}

void TitleBarAbstract::mouseDoubleClickEvent(QMouseEvent *event)
{
	if(window()->isMaximized()){
		emit normalize();
	}
	else{
		emit maximize();
	}
}
